/*******************************************************************
 * Copyright (c) [2025] [入门游戏开发版权所有]
 *
 * 创建日期: [2025-5-11]
 * 作者: [1024]
 *
 *
 * 版权所有。未经版权所有者明确书面许可，任何个人或组织
 * 不得复制、修改、分发或以其他方式使用本文件。
 * 仅供个人学习使用
 *
 * 联系方式:
 * Email: [addylines@163.com]
 ******************************************************************/
import WaterManager from './WaterManager'
cc.Class({
    extends: cc.Component,

    properties: {
        graphics: {
            default: null,
            type: cc.Graphics
        },
        water: cc.Node
    },

    onLoad() {
        // 绑定触摸事件
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    },

    onTouchStart(event) {

        let touchPos = event.getLocation();
        let localPos = this.node.convertToNodeSpaceAR(touchPos);

        this._pathPointList = [];

        const graphicsNode = new cc.Node();
        this.node.addChild(graphicsNode);

        this._curGraphics = graphicsNode.addComponent(cc.Graphics);
        this._curGraphics.strokeColor = cc.Color.BLACK;
        this._curGraphics.lineWidth = 5;

        const {
            x,
            y
        } = localPos
        this._curGraphics.moveTo(x, y);

        this._pathPointList.push(cc.v2(x, y));
    },

    onTouchMove(event) {
        let touchPos = event.getLocation();
        let localPos = this.node.convertToNodeSpaceAR(touchPos);

        // 获取当前触摸点的相对位置
        const {
            x,
            y
        } = localPos
        // 获取上一个点的位置
        const {
            x: preX,
            y: preY
        } = this._pathPointList[this._pathPointList.length - 1];
        // 计算两点之间的距离
        const diffX = x - preX;
        const diffY = y - preY;
        // 两点之间的距离的平方
        const dis = diffX * diffX + diffY * diffY;
        const lineWidth = this._curGraphics.lineWidth;
        // 两点之间的距离大于线宽的平方，才添加到路径点列表
        if (dis >= lineWidth * lineWidth) {
            const d = 0.001
            const curSlope = Math.abs(diffX) < d ? (Number.MAX_SAFE_INTEGER * Math.sign(diffX) * Math.sign(diffY)) : (diffY / diffX)
            if (this._pathPointList.length > 1) {
                const diffK = curSlope - this._preSlope;
                // 斜率相同去掉前一个点
                if (Math.abs(diffK) < d) {
                    this._pathPointList.pop()
                }
            }
            // 添加到路径点列表
            this._pathPointList.push(cc.v2(x, y))
            // 绘制路径
            this._curGraphics.lineTo(x, y)
            this._curGraphics.stroke();

            // 保存上一个点的斜率
            this._preSlope = curSlope;
        }
    },

    onTouchEnd(event) {
        // 绘制结束，添加刚体组件
        // 两个点以上才添加刚体组件
        if (this._pathPointList.length > 1) {
            console.log(this._pathPointList.length)
            this._curGraphics.addComponent(cc.RigidBody);
            // 添加多边形碰撞组件
            for (let index = 0; index < this._pathPointList.length - 1; index++) {
                const start = this._pathPointList[index];
                const end = this._pathPointList[index + 1];
                const polyCollider = this._curGraphics.addComponent(cc.PhysicsPolygonCollider);
                // 计算两点的方向向量
                const directVector = cc.v2(end.x - start.x, end.y - start.y).normalize();
                const widhtHalf = this._curGraphics.lineWidth / 2;
                // 计算多边形的四个点
                const p1 = cc.v2(directVector.y, -directVector.x).multiplyScalar(widhtHalf).add(cc.v2(start.x, start.y))
                const p2 = cc.v2(-directVector.y, directVector.x).multiplyScalar(widhtHalf).add(cc.v2(start.x, start.y))
                const p3 = cc.v2(directVector.y, -directVector.x).multiplyScalar(widhtHalf).add(cc.v2(end.x, end.y))
                const p4 = cc.v2(-directVector.y, directVector.x).multiplyScalar(widhtHalf).add(cc.v2(end.x, end.y))
                // 设置多边形碰撞组件的定点列表
                polyCollider.points = [p1, p2, p4, p3];
                // 让修改生效
                polyCollider.apply();
            }
            this.water.getComponent(WaterManager).startSpawnWater()
        } else {
            this._curGraphics.node.destroy();
        }
        // 重置
        this._curGraphics = null;
    },
});